#/usr/bin/python
# coding: utf8

import win32api
import win32con


def setRegValue(keystr, vname, value):
    '''
    设置某个注册表项的值，不存在则先添加项再设置值
    '''
    root = win32con.HKEY_CURRENT_USER
    key = win32api.RegOpenKey(root, keystr, 0, win32con.KEY_ALL_ACCESS)
    try:
        kv = win32api.RegQueryValueEx(key, vname)
        if(kv==value):
            return True
        else:
            win32api.RegSetValueEx(key, vname, 0, win32con.REG_SZ, value)
            win32api.RegCloseKey(key)
    except:
        win32api.RegSetValueEx(key, vname, 0, win32con.REG_SZ, value)
        win32api.RegCloseKey(key)
        return True


def proxy(enable):
    keystr = 'Software\Microsoft\Windows\CurrentVersion\Internet Settings'
    if enable:
        setRegValue(keystr, 'ProxyEnable', '1')
    else:
        setRegValue(keystr, 'ProxyEnable', '0')

proxy(True)
